package com.maple.common.utils.agreements;
import com.maple.common.utils.Tools;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class B5AgreementUtil2 {
    /**
     * 截取字符串
     * */
    public static void testRegexp(String s,CallBackWeight callBackWeight) {
        if (s != null && !"".equals(s)) {
            String regex = "02[0-9a-fA-F]{30}0D[0-9a-fA-F]{2}";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(s);
            while (matcher.find()) {
                String matcherString = matcher.group();
                System.out.println(matcherString);
                byte[]data=Tools.parseHexStr2Byte(matcherString);
                int sum=0;
                for (int i = 0; i < data.length - 2; i++) {
                    sum+=data[i];
                }
                System.out.println(Tools.integerToHexString(sum%256));

                int dian=(data[1]&7)-2;
//                System.out.println(data[2]&4);//B状态 bit2
//                System.out.println(data[1]&7-2);//小数点位
                /**状态字B校验**/
                boolean isJingzhong=(data[2]&1)!=0;//是否是净重
                boolean isFu=(data[2]&2)!=0;//单位是否为负
                boolean isChaozai=(data[2]&4)!=0;//是否超载
                boolean isKG=(data[2]&16)!=0;//单位
                System.out.println("净重："+isJingzhong+",负："+isFu+",超载："+isChaozai+",kg："+isKG);
                if (isFu||isChaozai){
                    continue;
                }
                /**获取重量*/
                byte[]data1= Arrays.copyOfRange(data,4,10);//毛重或净重
                callBackWeight.succeed(getData(dian,data1));
                /**获取重量*/
//                byte[]data2= Arrays.copyOfRange(data,10,16);//皮重
//                getData(dian,data2);

            }
        }
    }
    public static String getData(int dian,byte[]data){

        if (data.length!=6){
            return "";
        }
        StringBuilder weight = new StringBuilder();
        for (int i = 0; i < data.length; i++) {
            weight.append((char) data[i]);
        }
        if (dian>0){
            weight.insert(weight.length()-dian,".");
        }
        try {
            BigDecimal decimal=new BigDecimal(weight.toString().trim());
            System.out.println(decimal.toString());
            return decimal.toString();
        }catch (NumberFormatException e){
            System.out.println("数据解析错误");
            return "";
        }

    }
}
